<?php

class Wall_ListController extends Core_Controller_Action_Standard
{

    public function createAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        
        $form = new Wall_Form_List_Create();
        $this->view->form = $form;

        if (!$this->getRequest()->isPost())
        {
            return;
        }

        //If form is invalid, display form with errors
        if (!$form->isValid($this->getRequest()->getPost()))
        {
            return;
        }

        //Get form values
        $values = $form->getValues();
        $list_name = $values['list_name'];
        $selected_friends_array = explode(',', $values['friend_array']);
        $uids = array();
        foreach ($selected_friends_array as $friend_id)
        {
            if ($friend_id == '')
            {
                continue;
            }
            $uids[] = intval($friend_id);
        }
        
        //Process
        Engine_Api::_()->wall()->createList($viewer, $list_name, $uids);

        //Close popup and refresh
        $this->view->status = true;
        $this->view->message = Zend_Registry::get('Zend_Translate')->_('You have created a new list successfully');
        return $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh' => 10,
                    'messages' => Array($this->view->message)));
    }

    public function deleteAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        $list = Engine_Api::_()->getItem('list', $this->getRequest()->getParam('listid'));


        // In smoothbox
        $this->_helper->layout->setLayout('default-simple');

        $this->view->form = $form = new Wall_Form_List_Delete();

        if (!$list)
        {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_("List doesn't exists or not authorized to delete");
            return;
        }

        if (!$this->getRequest()->isPost())
        {
            $this->view->status = false;
            $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
            return;
        }

        if (!$form->isValid($this->getRequest()->getPost()))
        {
            return;
        }

        $db = $list->getTable()->getAdapter();
        $db->beginTransaction();

        try
        {
            $list->delete();
            $db->commit();
        }
        catch (Exception $e)
        {
            $db->rollBack();
            throw $e;
        }

        return $this->_forward('success', 'utility', 'core', array(
                    'messages' => array(Zend_Registry::get('Zend_Translate')->_('The list has been deleted.')),
                    'layout' => 'default-simple',
                    'parentRefresh' => 10,
                ));
    }

    public function editAction()
    {
        if (!$this->_helper->requireUser()->isValid())
            return;

        $lists_table = Engine_Api::_()->getDbtable('lists', 'wall');
        $items_table = Engine_Api::_()->getDbtable('listItems', 'wall');

        $list_id = $this->_getParam('listid');
        if ($list_id == null)
            return;

        //Get selected friends
        $items_select = $items_table->select()->where('list_id = ?', $list_id);

        $selected_friends = array();
        foreach ($items_table->fetchAll($items_select) as $item)
        {
            $selected_friends[] = $item->object_id;
        }
        $selected_friends = join(',', $selected_friends);

        $list = $lists_table->find($list_id)->current()->toArray();

        $this->view->form = $form = new Wall_Form_List_Edit(array(
                    'listName' => $list['list_name'],
                    'selectedFriends' => $selected_friends
                ));
        
        if (!$this->getRequest()->isPost())
        {
            return;
        }

        //If form is invalid, display form with errors
        if (!$form->isValid($this->getRequest()->getPost()))
        {
            $values = $form->getValues();
            if ($values['list_name'] != $list['list_name']){
                return;
            }
        }

        //Get form values
        $values = $form->getValues();
        $list_name = $values['list_name'];
        $selected_friends_array = explode(',', $values['friend_array']);
        $uids = array();
        foreach ($selected_friends_array as $friend_id)
        {
            if ($friend_id == '')
            {
                continue;
            }
            $uids[] = $friend_id;
        }
        
        //Process
        Engine_Api::_()->wall()->updateList($viewer, $list_id, $list_name, $uids);
        
        //Close popup and refresh
        $this->view->status = true;
        $this->view->message = Zend_Registry::get('Zend_Translate')->_('You have edited the list successfully');
        return $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh' => 10,
                    'messages' => Array($this->view->message)));
    }

}
